<template>
  <div id="home">
    <NarBar class="home-nav">
      <div slot='center'>购物街</div>
    </NarBar>
    <TabControl :titles="['流行','新款','精选']" class="tab-control" @tabClick="tabClick" ref='TabControl1'
      v-show='isTabFixed'></TabControl>
    <Scroll class='content' ref="scroll" :probeType='3' @srcoll='contentSrcoll' :pullUpLoad='true'
      @pullingUp='loadMore'>
      <HomeSwiper :banners='banners' @swiperImageLoad='swiperImageLoad'></HomeSwiper>
      <HomeRecommend :recommends='recommends'></HomeRecommend>
      <Feature></Feature>
      <TabControl :titles="['流行','新款','精选']" class="tab-control" @tabClick="tabClick" ref='TabControl2'></TabControl>
      <GoodsList :goods="showGoods"></GoodsList>
    </Scroll>
    <BackTop @click.native="backClick" v-show="isShowBackTop"></BackTop>
  </div>
</template>

<script>
  import HomeSwiper from './childComps/HomeSwiper.vue'
  import HomeRecommend from './childComps/HomeRecommend.vue'
  import Feature from './childComps/FeatureView.vue'

  import NarBar from 'components/common/navbar/NavBar.vue'
  import TabControl from 'components/content/tabControl/TabControl.vue'
  import GoodsList from 'components/content/gooods/GoodsList.vue'
  import Scroll from 'components/common/scroll/Scroll.vue'
  import BackTop from 'components/content/backTop/BackTop.vue'

  import {
    getHomeMultidata,
    getHomeGoods
  } from 'network/home.js'



  import {
    debounce
  } from 'common/utils.js'
  import {
    itemListener
  } from 'common/mixin.js'



  export default {
    name: 'Home',
    components: {
      HomeSwiper,
      HomeRecommend,
      Feature,
      NarBar,
      TabControl,
      GoodsList,
      Scroll,
      BackTop
    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list
      }
    },
    mixins: [itemListener],
    data() {
      return {
        banners: [],
        recommends: [],
        //设置请求数据的模型
        goods: {
          'pop': {
            page: 0,
            list: []
          },
          'new': {
            page: 0,
            list: []
          },
          'sell': {
            page: 0,
            list: []
          },
        },
        currentType: 'pop',
        tabOffsetTop: 0,
        isTabFixed: false,
        saveY: 0,
        isShowBackTop: false
      }
    },
    destroyed() {

    },
    activated() {
      this.$refs.scroll.refresh()
      this.$refs.scroll.scrollTo(0, this.saveY, 0)
    },
    deactivated() {
      //保存Y值  可以滚动到某个位置出现置顶符号
      this.saveY = this.$refs.scroll.getScrolly()

      // 取消全局事件的监听
      this.$bus.$off('itemImageLoad', this.itemImgListrner)
    },
    created() {
      this.getHomeMultidata()
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')

    },
    mounted() {
      // this.tabOffsetTop = this.$refs.TabControl2.$el.offsetTop  //位置获取不正确的
    },

    methods: {
      swiperImageLoad() {

        this.tabOffsetTop = this.$refs.TabControl2.$el.offsetTop
      },
      contentSrcoll(position) {
        this.isShowBackTop = (-position.y) > 1000
        this.isTabFixed = -position.y > this.tabOffsetTop
      },
      loadMore() {
        this.getHomeGoods(this.currentType)
      },
      backClick() {
        this.$refs.scroll.scrollTo(0, 0)
      },
      //事件监听
      tabClick(index) {
        switch (index) {
          case 0:
            this.currentType = 'pop'
            break
          case 1:
            this.currentType = 'new'
            break
          case 2:
            this.currentType = 'sell'
            break
        }
        this.$refs.TabControl1.currentIndex = index
        this.$refs.TabControl2.currentIndex = index
      },
      //网络请求
      getHomeMultidata() {
        getHomeMultidata().then(res => {
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        })
      },
      getHomeGoods(type) {
        const page = this.goods[type].page + 1;
        getHomeGoods(type, page).then(res => {
          this.goods[type].list.push(...res.data.list);
          this.goods[type].page += 1
          this.$refs.scroll.finishPullUp()
        })
      }
    }
  }
</script>

<style scoped>
  #home {
    /* padding-top: 2.75rem; */
    height: 100vh;
    position: relative;
  }

  .home-nav {
    background-color: hotpink;
    color: #fff;
    /*       position: fixed;
    z-index: 9;
    left: 0;
    right: 0;
    top: 0; */
  }

  .tab-control {
    position: relative;
    z-index: 9;
  }

  .content {
    overflow: hidden;
    position: absolute;
    top: 2.75rem;
    bottom: 3.0625rem;
    left: 0;
    right: 0;
  }
</style>
